const { comment } = require("../model/index")
const { Op } = require("sequelize");
class CommentService {
    async addComment(data) {
        let { username, phone, content } = data
        try {
            await comment.create({
                username,
                phone,
                content,
                create_time:new Date()
            })
        } catch (error) {
            console.log(error)
        }
    }
    async getComment() {
        try {
            let res = await comment.findAll()
            return res
        } catch (error) {
            console.log(error)
        }
    }
    async deleteCommentById(id) {
        try {
            await comment.destroy({
                where: {
                    id
                }
            }) 
        } catch (error) {
            console.log(error)
        }
    }
    async searchCommentByName(username) {
        try {
            if (!username) {
                let res = await comment.findAll()
                return res
            }
            let res=await comment.findAll({
                where: {
                    username: {
                        [Op.substring]: username
                    }
                }
            })
            return res
        } catch (error) {
            console.log(error)
        }
    }
}
module.exports=new CommentService()